from django import template
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage


register = template.Library()

@register.inclusion_tag('translations/comments/partial_article_comments.html')
def render_article_comments(request, article):
    comments = article.comments.all()

    paginator = Paginator(comments, 20)
    page = request.GET.get('comment_page')
    try:
        comments = paginator.page(page)
    except PageNotAnInteger:
        comments = paginator.page(1)
    except EmptyPage:
        comments = paginator.page(paginator.num_pages)

    return {'request': request, 'user': request.user, 'article': article, 'comments': comments}
